import es

from wcs.libs.players import players
from wcs.libs.items import items

itemName = 'Mask of Death'
item = items.addItem(itemName)
item.setGroup("attack")
item.setCost(4000)
item.setDescription("Gain health from the damage you do to others")
item.aliveToUse = True
item.setMaxCount(1)
        
def player_hurt(event_var):
    """
    Executed when a player is hurt. If the attacker has this item, then ensure
    that he is healed for 20% of the damage given.
    """
    attacker = int(event_var["attacker"]) if event_var["attacker"] else 0
    if attacker:
        player = players[attacker]
        if not player.getItem(itemName).count:
            return
        if event_var["es_userteam"] == event_var["es_attackerteam"]:
            return
        if event_var["damage"]:
            damage = int(event_var["damage"])
        elif event_var["dmg_health"]:
            damage = int(event_var["dmg_health"])
        else:
            damage = 0
        amount = int(damage * 0.2)
        if not amount:
            return
        player.heal(amount)
        text = ("#greenMask of Death #default- #lightgreenYou have leached " +
            "#green%s #lightgreenhealth from #green%s.")
        es.tell(attacker, "#multi", text % (amount, event_var['es_username']))
